#include "showkeyWindow.h"

ShowkeyWindow::ShowkeyWindow(const QRect &rect, QWidget *parent)
    : QWidget(parent),
      m_VLayout(nullptr),
      m_HLayout(nullptr),
      m_Timer(nullptr)
{  
    m_styleSettings = new QGSettings("org.ukui.style");
    setFixedSize(rect.width(), rect.height());
    setGeometry(rect.x(), rect.y(), rect.width(), rect.height() );
    setWindowFlags( Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::ToolTip );
    setAttribute(Qt::WA_TranslucentBackground);     //实现背景透明

    //  QFont qfont;;
    //  qfont.setPixelSize( keyWindowHeight - 20 );
    //  keyLabel->setFont( qfont );

    m_Timer = new QTimer(this);
    connect( m_Timer, SIGNAL(timeout()), this, SLOT(resetLayout()));

    m_VLayout = new QVBoxLayout(this);
    m_VLayout->addStretch();
}

void ShowkeyWindow::addText(QString value)
{
    m_Timer->start(150);
    QString themeStyle = m_styleSettings->get("styleName").toString();
    if (m_HLayout)
    {
        QLabel *newLabel = new QLabel(this);
        if ("ukui-dark" == themeStyle || "ukui-black" == themeStyle) {
            newLabel->setStyleSheet("QLabel{border: 1px solid rgb(20, 20, 20);background-color: rgb(20, 20, 20); border-radius:6px}");
        } else {
            newLabel->setStyleSheet("QLabel{border: 1px solid #e6eeef;background-color: #e6eeef; border-radius:6px}");
        }
        newLabel->setFixedSize(50, 40);
        newLabel->setAlignment(Qt::AlignCenter);
        newLabel->setText(value);
        m_HLayout->insertWidget(1, newLabel, 0, Qt::AlignLeft);
        nRowLabelWidth = nRowLabelWidth + newLabel->width() + 20;
        if (nRowLabelWidth > width() - 5)
        {
            nRowLabelWidth = 0;
            m_HLayout = nullptr;
            m_Timer->stop();
        }
    }
    else
    {
        if (nColLabelHeight > height() - 30)
        {
            nColLabelHeight = 0;
            clearShow();
            m_Timer->stop();
        }

        QWidget *newWidget = new QWidget(this);
        widgetList.append(newWidget);
        m_HLayout = new QHBoxLayout(newWidget);
        m_HLayout->setContentsMargins(5, 0, 0, 8);
        QLabel *newLabel = new QLabel(newWidget);
        if ("ukui-dark" == themeStyle || "ukui-black" == themeStyle) {
            newLabel->setStyleSheet("QLabel{border: 1px solid rgb(20, 20, 20);background-color: rgb(20, 20, 20); border-radius:6px}");
        } else {
            newLabel->setStyleSheet("QLabel{border: 1px solid #e6eeef;background-color: #e6eeef; border-radius:6px}");
        }
        newLabel->setFixedSize(50, 40);
        newLabel->setAlignment(Qt::AlignCenter);
        newLabel->setText(value);
        m_HLayout->addWidget(newLabel, 0, Qt::AlignLeft);
        m_HLayout->addStretch();

        nColLabelHeight = nColLabelHeight + newWidget->height() + 20;
        m_VLayout->addWidget(newWidget, 0, Qt::AlignBottom);
    }
}

void ShowkeyWindow::clearShow()
{
    foreach (QWidget *pWidget, widgetList) {
        pWidget->setVisible(false);
    }
}

void ShowkeyWindow::resetLayout()
{
    m_HLayout = nullptr;
}

ShowkeyWindow::~ShowkeyWindow()
{
}

